
*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 

*------------------------------------------------------------------------------*
*	Step 8- Matched diffindiff	   *
*------------------------------------------------------------------------------*	


*Run the FE regression of real util per sqft on treat*post 
set more off 

	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 

	**DIFF IN DIFF 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`i'_wmatches.dta", clear 
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

	
	*label variables for regression tables 	
	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	
	*Run the FE regression of real rent per sqft on treat*post 
	local Covariates "Cov0 Cov1 Cov2 Cov3" 
	foreach Cov of local Covariates{
	eststo RentFE1: reghdfe logrealrentpersf interaction i.treat, absorb(i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFE1, labels(year "Year FE" city_cat#year "City by Year FE")
	eststo RentCov1_FE: reghdfe logrealrentpersf interaction i.treat $Cov1, absorb(i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentCov1_FE, labels(year "Year FE" city_cat#year "City by Year FE")
	eststo RentFE`Cov': reghdfe logrealrentpersf interaction $`Cov', absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFE`Cov', labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	}
	
		
	*make Latex Table 
	esttab RentFE1 RentCov1_FE RentFECov1 RentFECov3 using "$results/04_rentpersf_matched`i'.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Energy Star Certification and Rent Per Sq. Ft. \label{tab01rentpersfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	

*using hdfe 
	local Covariates "Cov0 Cov1 Cov2 Cov3" 
	foreach Cov of local Covariates{
	eststo UtilFE1: reghdfe logrealutilpersf interaction i.treat, absorb(i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFE1, labels(year "Year FE" city_cat#year "City by Year FE")
	eststo UtilCov1_FE: reghdfe logrealutilpersf interaction i.treat $Cov1 , absorb(i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilCov1_FE, labels(year "Year FE" city_cat#year "City by Year FE")
	eststo UtilFE`Cov': reghdfe logrealutilpersf interaction rl_yr_rentpersf $`Cov' , absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFE`Cov', labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	}
	
			
	*make Latex Table 
	esttab UtilFE1 UtilCov1_FE UtilFECov1 UtilFECov3 using "$results/05_utilpersf_matched`i'.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Energy Star Certification and Utility Expenditure Per Sq. Ft. \label{tab02utilpersfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})
	

	**Robustness for main specification with zipcode by year Fixed effects 
	
	*Run the FE regression of real rent per sqft on treat*post 
	local Covariates "Cov0 Cov1 Cov2 Cov3" 
	foreach Cov of local Covariates{
	eststo RentFE`Cov': reghdfe logrealrentpersf interaction $`Cov', absorb(i.propnum i.zip#i.year) vce(cluster cbsa)
	estfe  RentFE`Cov', labels(year "Year FE" zip#year "Zip Code by Year FE" propnum "Property FE")
	
	eststo UtilFE`Cov': reghdfe logrealutilpersf interaction rl_yr_rentpersf $`Cov' , absorb(i.propnum i.zip#i.year) vce(cluster cbsa)
	estfe  UtilFE`Cov', labels(year "Year FE" zip#year "Zip Code by Year FE" propnum "Property FE") 
	
	}
	
		
	*make Latex Table 
	esttab RentFECov1 UtilFECov1 using "$results/08_rentandutilpersf_matched`i'_zipfe.tex" , label replace booktabs ///
	alignment(SS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Energy Star Certification and Rent and Utilities Per Sq. Ft. \label{tab08rentandutilpersfmatched`i'zipfe}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})
	
	} 
	
	***Robustness test with multiple matched dataset altnernatives 
	**This is for files 2-10 
	forvalues i=2/14{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`i'_wmatches.dta", clear 
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

	
	*label variables for regression tables 	
	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	*Run the FE regression of real rent per sqft on treat*post 
	local Covariates "Cov1" 
	foreach Cov of local Covariates{
	eststo RentFE`Cov': reghdfe logrealrentpersf interaction $`Cov', absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	*estfe  RentFE`Cov', labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	}
	
	*Save the output from the rent regression into a stata datafile 
	regsave interaction using "$results/08_matchingrobustnessresults`i'.dta", addlabel(specification`i', specification`i') table(Rent, order(regvars) format(%5.3f) parentheses(stderr) asterisk(5)) replace
	

*using hdfe 
	local Covariates "Cov1" 
	foreach Cov of local Covariates{
	eststo UtilFE`Cov': reghdfe logrealutilpersf interaction rl_yr_rentpersf $`Cov' , absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	*estfe  UtilFE`Cov', labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	}
	
	*Save the output from the utility regression into a stata datafile 
	regsave interaction using "$results/08_matchingrobustnessresults`i'.dta", addlabel(specification`i', specification`i')  table(Utilities, order(regvars) format(%5.3f) parentheses(stderr) asterisk(5)) append
	

	} 
	
	/*

Notes on the different matched datasets and how they were matched 
2. Time invariant characteristics 
3. Mean of time-varying characteristics 
4. Matching on time-invariant characteristics and mean of time-varying characteristics (2 and 3 combined)
5. Matching on time-invariant characteristics only before 2009 the "treatment" year (drop all obesrvations after 2009)
6. Matching on time-invariant characteristics only before 2008 the "treatment" year (drop all obesrvations after 2008)
7. Matching on mean of timevarying cahracteristics before 2009  only, dropping all data after 2009. This means we are only matching units that are in the sample before 2009 
8. Matching on both time-invariant and time-varying characteristics before 2009 only 
9. Same as 7 with 2008 data 
10. Same as 8 with 2008 data 
11. Same as 2 with replacement 
12. Same as 2 with calipers equal to 2 SD 
13. Same as 2 with calipers equal to 0.5 SD
14. Same as 2 without calipers  

(1) Time invariant characteristics 
(2) Mean of time-varying characteristics 
(3) Matching on time-invariant characteristics and mean of time-varying characteristics (1 and 2 combined)
(4) Matching on time-invariant characteristics only before 2009 the "treatment" year (drop all observations after 2009)
(5) Matching on time-invariant characteristics only before 2008 the "treatment" year (drop all observations after 2008)
(6) Matching on mean of time-varying characteristics before 2009  only, dropping all data after 2009. This means we are only matching units that are in the sample before 2009 
(7) Matching on both time-invariant and time-varying characteristics before 2009 only 
(8) Same as 6 with 2008 data 
(9) Same as 7 with 2008 data 
(10) Same as 1 with replacement 
(11) Same as 1 with calipers equal to 2 SD 
(12) Same as 1 with calipers equal to 0.5 SD
(13) Same as 1 without calipers

*/


/*


TIme invariant characteristics are 
"mean_nooffloors", "mean_noofunits", "mean_noofbuildings", "mean_nra", "mean_sqft", "mean_yrbuilt", 
            "mean_lastrenovatedyear",  "mean_yrbuiltorlastren",  "mean_Covered_E",  "mean_latitude", "mean_longitude") 


*Time varying characteristics are 
"mean_sqft",  "mean_yrbuilt",  "mean_Covered_E",  
            "mean_real_elecprice",  "mean_real_gasprice", "mean_Unemployment", "mean_HDD",  "mean_CDD",  "mean_age", "mean_used_space",
            "mean_percentleased",  "mean_real_capex_ti", "mean_real_capex_bldimp", "mean_dFundType1",
            "mean_dFundType2", "mean_dFundType3", "mean_dFundType4", "mean_dFundType5", "mean_dFundType6")
			

*/

	
	*import the data with all results and append sets together 
	*to export to latex table 
	use  "$results/08_matchingrobustnessresults2.dta", clear 
	append using "$results/08_matchingrobustnessresults3.dta"
	append using "$results/08_matchingrobustnessresults4.dta"
	append using "$results/08_matchingrobustnessresults5.dta"
	append using "$results/08_matchingrobustnessresults6.dta"
	append using "$results/08_matchingrobustnessresults7.dta"
	append using "$results/08_matchingrobustnessresults8.dta"
	append using "$results/08_matchingrobustnessresults9.dta"
	append using "$results/08_matchingrobustnessresults10.dta"
 	append using "$results/08_matchingrobustnessresults11.dta"
	append using "$results/08_matchingrobustnessresults12.dta"
	append using "$results/08_matchingrobustnessresults13.dta"
	append using "$results/08_matchingrobustnessresults14.dta"
	
	*Format dataset into table to export into latex 
	drop if var == "r2"
	gen Obs = Rent if var == "N"
	replace Obs = Obs[_n+1] if Obs == ""
	replace Obs = Obs[_n+1] if Obs == ""
	replace Obs = "" if var == "interaction_stderr"
	drop if var == "N"
	
	local obsnum = _N
	di "`obsnum'"
	local setobs = `obsnum' + 1 
	di "`setobs'"
	set obs  `setobs'
	
	replace var = "\underline{Test 1}" if var == "" 
	forvalues i = 2/14{
		replace var = "\underline{Test `i'}" if var == "specification`i'"	
		replace Rent = "" if Rent == "specification`i'"
		replace Utilities = "" if Utilities == "specification`i'"
	}
	replace var = "Cert*Post" if var == "interaction_coef"
	replace var = "" if var == "interaction_stderr"
	

	gen sortorder = _n 
	replace sortorder = 0 if sortorder == _N 
	sort sortorder 
	drop if var == "\underline{Test 14}" 
	
	loc j = 1 
	forvalues i = 1/13{
		replace var = "\underline{Test `i':}" + " " + var if sortorder == `j'
		local j =`j'+3
	}
	
	forvalues i = 1/13{
		drop if var == "\underline{Test `i'}"
	}
	
	*start writing latex table 
	global filename 08_diffindiff_matched_robustnesstable
	capture file close table1		
	file open table1 using "$results/$filename.tex", write text replace

file write table1 "\begin{table}[htbp]\centering" _n
file write table1 "\caption{Matched Samples: Robustness Tests\label{08diffindiffmatchedrobustnesstable}}" _n
file write table1 "\vspace{-2mm}" _n
file write table1 "\begin{tabular}{@{\extracolsep{4pt}}lcccc@{}ccc}" _n //this is where you change justification. Options are l c r
*extracolsep from http://tex.stackexchange.com/questions/101000/how-to-shorten-cline-so-that-multiple-groups-in-a-table-become-visible

file write table1 "\hline\hline \\ [-.9em]" _n 

**This is where you put the column labels in. I have them set up as two rows each. You can also do one row and just fill in
**the top or bottom ones. 

local col1_top "Robustness Test"
local col1_bottom ""
local col2_top "Rent per Sqft."
local col2_bottom ""
local col3_top "Utilities per Sqft."
local col3_bottom ""
local col4_top "Obs."
local col4_bottom ""



local col1 "\multicolumn{1}{c}{$\begin{matrix}\text{`col1_top'}\\ \text{`col1_bottom'} \end{matrix}$}"
local col2 "\multicolumn{1}{c}{$\begin{matrix}\text{`col2_top'}\\ \text{`col2_bottom'} \end{matrix}$}"
local col3 "\multicolumn{1}{c}{$\begin{matrix}\text{`col3_top'}\\ \text{`col3_bottom'} \end{matrix}$}"
local col4 "\multicolumn{1}{c}{$\begin{matrix}\text{`col4_top'}\\ \text{`col4_bottom'} \end{matrix}$}"




file write table1 "  & (1) & (2) & (3)  \\" _n



file write table1 " `col1_top' & `col2_top' & `col3_top' & `col4_top'  	\\" _n


file write table1 "\hline \\" _n



capture gen row = _n

*this is where the data is actually written in to the table
*You have to adjust the things to the right of [`r'] below to the variable names you want

gen id = _n

levelsof id, local(vars)
foreach v of local vars  {
  qui sum row if id==`v'
  
	local r = r(mean)
	local t1 = var[`r']
	local t2 = Rent[`r']
	local t3 = Utilities[`r']
	local t4 = Obs[`r'] 
	
	
	
	file write table1 "`t1' & `t2' & `t3' & `t4'   \\" _n
}
*

*it's possible to put in summary stats below or whatever you want really
	



file write table1 "\hline" _n
file write table1 "\end{tabular}" _n
file write table1 "\caption*{{\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level. The 13 robustness tests are: (1) Time invariant characteristics (2) Mean of time-varying characteristics (3) Matching on time-invariant characteristics and mean of time-varying characteristics (1 and 2 combined) (4) Matching on time-invariant characteristics only before 2009 the ``treatment'' year (drop all observations after 2009) (5) Matching on time-invariant characteristics only before 2008 the ``treatment'' year (drop all observations after 2008) (6) Matching on mean of time-varying characteristics before 2009  only, dropping all data after 2009. This means we are only matching units that are in the sample before 2009  (7) Matching on both time-invariant and time-varying characteristics before 2009 only  (8) Same as 6 with 2008 data (9) Same as 7 with 2008 data (10) Same as 1 with replacement (11) Same as 1 with calipers equal to 2 SD (12) Same as 1 with calipers equal to 0.5 SD (13) Same as 1 without calipers}}" _n
file write table1 "\end{table}" _n


file close table1		

	
	
	**DIFF IN DIFF for osbervations without the imputations
	forvalues i=15/15{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`i'_wmatches.dta", clear 
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

	
	*label variables for regression tables 	
	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 

	**Robustness for main specification with data only for observations 
	*without at least one quarter filled in 
	
	*Run the FE regression of real rent per sqft on treat*post 
	local Covariates "Cov0 Cov1 Cov2 Cov3" 
	foreach Cov of local Covariates{
	eststo RentFE`Cov': reghdfe logrealrentpersf interaction $`Cov', absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFE`Cov', labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	
	eststo UtilFE`Cov': reghdfe logrealutilpersf interaction rl_yr_rentpersf $`Cov' , absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFE`Cov', labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	
	}
	
		
	*make Latex Table 
	esttab RentFECov1 UtilFECov1 using "$results/08_rentandutilpersf_matched`i'.tex" , label replace booktabs ///
	alignment(SS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Energy Star Certification and Rent and Utilities Per Sq. Ft. \label{tab08rentandutilpersfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})
	
	} 
	
	
	
 
	
	*Het effects by energy star score 
	set more off 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
	*Generate Variable for Intensity of Treatment 
	bysort propnum: gen rate=everrated
	*Generate different bins for rating 	
	gen rate1=1 if rate>=75 & rate<81 
	replace rate1=0 if missing(rate1)
	gen rate2=1 if rate>=81 & rate<100 
	replace rate2=0 if missing(rate2)                                                                                              
	*Generate a catogory for these so that I can plot them 
	gen rate_round = round(rate)
	
	gen rate_cat=. 
	replace rate_cat=1 if rate1==1 
	replace rate_cat=2 if rate2==1 
	
*Diff in diff regression with Het treatment effects 
	
	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	*Generate the heterogenous treatment effects interaction 
	gen interaction_1 = treat*post*rate1 
	gen interaction_2 = treat*post*rate2 
	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_2 rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction interaction_2 $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	
	*Testing whether there are statistically different treatment effects between the high and low scores 
	eststo UtilFECov1_het_test: reghdfe logrealutilpersf interaction interaction_2 rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het_test, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het_test: reghdfe logrealrentpersf interaction interaction_2 $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het_test, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	label var interaction "Cert*Post" 
	label var interaction_1 "1(Cert*Post*\$<=\$Median)"
	label var interaction_2 "1(Cert*Post*\$>\$Median)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	*make Latex Table 
	esttab RentFECov1_het UtilFECov1_het using "$results/06_diffindiff_het_matched`i'.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Energy Star Score \label{tab04heteffectsfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	*make Latex Table 
	esttab RentFECov1_het_test UtilFECov1_het_test using "$results/06_diffindiff_het_matched`i'_test.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Energy Star Score \label{appendixtab04heteffectsfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	
	}
	
	
**Het effects by square footage 
	set more off 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'.dta", clear
	*median square footage is 218254
	
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

	*Generate different bins for rating 	
	gen sqft1=(sqft<=218254) 
	gen sqft2=(sqft>218254) 

	
	*Generate a catogory for these so that I can plot them 
	gen sqft_cat=. 
	replace sqft_cat=1 if sqft1==1 
	replace sqft_cat=2 if sqft2==1 
	
*Diff in diff regression with Het treatment effects 

	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	*Generate the heterogenous treatment effects interaction 
	gen interaction_1 = treat*post*sqft1 
	gen interaction_2 = treat*post*sqft2 
	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_2 rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction interaction_2 $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	label var interaction "Cert*Post" 
	label var interaction_1 "1(Cert*Post*\$<= \$ Median)"
	label var interaction_2 "1(Cert*Post* \$ > \$ Median)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)"
	
	*make Latex Table 
	esttab RentFECov1_het UtilFECov1_het using "$results/08_diffindiff_hetsqft_matched`i'.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Square Footage \label{tab05heteffectssqftfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	}
	
	*Het effects by building age
	set more off 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'.dta", clear	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

	*Generate different bins for rating 	
	gen age1=(age<=19) 
	gen age_2=(age>19) 

	
*Diff in diff regression with Het treatment effects 

	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft real_capex_bldimp_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	
	*Generate the heterogenous treatment effects interaction 
	gen interaction_1 = treat*post*age1 
	gen interaction_2 = treat*post*age_2 
	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_2 rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction interaction_2 $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")

	label var interaction "Cert*Post" 
	label var interaction_1 "1(Cert*Post*\$<= \$ Median)"
	label var interaction_2 "1(Cert*Post* \$ > \$ Median)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)"
	
	*make Latex Table 
	esttab RentFECov1_het UtilFECov1_het using "$results/14_diffindiff_hetage_matched`i'.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Building Age\label{tab06heteffectsagefmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	}
	
*Het effects by capital expenditure on Building improvements 
	set more off 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
	*Generate het TE for buildings with positive capex_bldimp in the 3 years leading up to treatment 
	
	*Generate years pre/post cert 
		gen years_pre_post_cert = year - firstyearrated 
	*Generate an indicator for positive capital expenditure in the 3 years before cert 
		gen d_capex_bldimp = . 
		replace d_capex_bldimp = 1 if years_pre_post_cert>=-3 & years_pre_post_cert<=0 & !missing(years_pre_post_cert) & capex_bldimp >0 
		bysort propnum: carryforward d_capex_bldimp, replace 
		replace d_capex_bldimp = 0 if missing(d_capex_bldimp) 
		
		gen d_capex_bldimp_median = . 
		replace d_capex_bldimp_median = 1 if years_pre_post_cert>=-3 & years_pre_post_cert<=0 & !missing(years_pre_post_cert) & capex_bldimp >=  314214
		bysort propnum: carryforward d_capex_bldimp_median, replace 
		replace d_capex_bldimp_median = 0 if missing(d_capex_bldimp_median)
		
		
	*Generate interacbldimpon variables for treatment status 
		gen interaction_d_capex_bldimp = interaction*d_capex_bldimp 
		gen interaction_d_capex_bldimp_med = interaction*d_capex_bldimp_median 
		
*Diff in diff regression with Het treatment effects 
	
	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	

	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_d_capex_bldimp rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo UtilFECov1_het1: reghdfe logrealutilpersf interaction interaction_d_capex_bldimp_med rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction  interaction_d_capex_bldimp $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het1: reghdfe logrealrentpersf interaction  interaction_d_capex_bldimp_med $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	
	label var interaction "Cert*Post" 
	label var interaction_d_capex_bldimp "1(Cert*Post*Exp.)"
	label var interaction_d_capex_bldimp_med  "1(Cert*Post*\$>\$Med. Exp.)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	*make Latex Table 
	esttab RentFECov1_het RentFECov1_het1 UtilFECov1_het UtilFECov1_het1 using "$results/15_diffindiff_hetcapexbldimp_matched`i'.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Capital Expenditures for Building Improvements\label{tab15heteffectsfmatched`i'}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	}
	

	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
	*Generate het TE for buildings with positive capex_bldimp in the 3 years leading up to treatment 
	
	*Generate years pre/post cert 
		gen years_pre_post_cert = year - firstyearrated 
	*Generate an indicator for positive capital expenditure in the 3 years before cert 
		gen d_capex_bldimp = . 
		replace d_capex_bldimp = 1 if years_pre_post_cert>=-2 & years_pre_post_cert<=0 & !missing(years_pre_post_cert) & capex_bldimp >0 
		bysort propnum: carryforward d_capex_bldimp, replace 
		replace d_capex_bldimp = 0 if missing(d_capex_bldimp) 
		
		gen d_capex_bldimp_median = . 
		replace d_capex_bldimp_median = 1 if years_pre_post_cert>=-2 & years_pre_post_cert<=0 & !missing(years_pre_post_cert) & capex_bldimp >=  314214
		bysort propnum: carryforward d_capex_bldimp_median, replace 
		replace d_capex_bldimp_median = 0 if missing(d_capex_bldimp_median)
		
		
	*Generate interacbldimpon variables for treatment status 
		gen interaction_d_capex_bldimp = interaction*d_capex_bldimp 
		gen interaction_d_capex_bldimp_med = interaction*d_capex_bldimp_median 
		
*Diff in diff regression with Het treatment effects 
	
	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	

	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_d_capex_bldimp rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo UtilFECov1_het1: reghdfe logrealutilpersf interaction interaction_d_capex_bldimp_med rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction  interaction_d_capex_bldimp $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het1: reghdfe logrealrentpersf interaction  interaction_d_capex_bldimp_med $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	
	label var interaction "Cert*Post" 
	label var interaction_d_capex_bldimp "1(Cert*Post*Exp.)"
	label var interaction_d_capex_bldimp_med  "1(Cert*Post*\$>\$Med. Exp.)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	*make Latex Table 
	esttab RentFECov1_het RentFECov1_het1 UtilFECov1_het UtilFECov1_het1 using "$results/15_diffindiff_hetcapexbldimp_matched`i'_2years.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Capital Expenditures for Building Improvements\label{tab15heteffectsfmatched`i'_2years}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	}
	
		forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_postmatch`i'_wmatches.dta", clear
	
	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	
	*Generate het TE for buildings with positive capex_bldimp in the 4 years leading up to treatment 
	
	*Generate years pre/post cert 
		gen years_pre_post_cert = year - firstyearrated 
	*Generate an indicator for positive capital expenditure in the 4 years before cert 
		gen d_capex_bldimp = . 
		replace d_capex_bldimp = 1 if years_pre_post_cert>=-4 & years_pre_post_cert<=0 & !missing(years_pre_post_cert) & capex_bldimp >0 
		bysort propnum: carryforward d_capex_bldimp, replace 
		replace d_capex_bldimp = 0 if missing(d_capex_bldimp) 
		
		gen d_capex_bldimp_median = . 
		replace d_capex_bldimp_median = 1 if years_pre_post_cert>=-4 & years_pre_post_cert<=0 & !missing(years_pre_post_cert) & capex_bldimp >=  314214
		bysort propnum: carryforward d_capex_bldimp_median, replace 
		replace d_capex_bldimp_median = 0 if missing(d_capex_bldimp_median)
		
		
	*Generate interacbldimpon variables for treatment status 
		gen interaction_d_capex_bldimp = interaction*d_capex_bldimp 
		gen interaction_d_capex_bldimp_med = interaction*d_capex_bldimp_median 
		
*Diff in diff regression with Het treatment effects 
	
	set more off 
	*List of covariates 
	#d ;
	global Cov0 	""; 
	global Cov1 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s
	 percentleased real_capex_ti_sqft "; 
	global Cov2 	"Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s
	 age age2 percentleased used_space1000s
	dFundType1 dFundType2 dFundType4 dFundType5 dFundType6";
	global Cov3	"Covered_E real_elecprice Unemployment HDD1000s CDD1000s 
	age age2 used_space1000s"; 
	#d cr 
	

	*Diff in diff 
	
	eststo UtilFECov1_het: reghdfe logrealutilpersf interaction interaction_d_capex_bldimp rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo UtilFECov1_het1: reghdfe logrealutilpersf interaction interaction_d_capex_bldimp_med rl_yr_rentpersf $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  UtilFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het: reghdfe logrealrentpersf interaction  interaction_d_capex_bldimp $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	eststo RentFECov1_het1: reghdfe logrealrentpersf interaction  interaction_d_capex_bldimp_med $Cov1, absorb(i.propnum i.city_cat#i.year) vce(cluster cbsa)
	estfe  RentFECov1_het1, labels(year "Year FE" city_cat#year "City by Year FE" propnum "Property FE")
	
	label var interaction "Cert*Post" 
	label var interaction_d_capex_bldimp "1(Cert*Post*Exp.)"
	label var interaction_d_capex_bldimp_med  "1(Cert*Post*\$>\$Med. Exp.)"
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	
	*make Latex Table 
	esttab RentFECov1_het RentFECov1_het1 UtilFECov1_het UtilFECov1_het1 using "$results/15_diffindiff_hetcapexbldimp_matched`i'_4years.tex" , label replace booktabs ///
	alignment(SSSS) ///
	b(%12.3f) se(%12.3f) star(* 0.05) /// sets format of parameters, standard errors, and stars
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	nonotes nogaps /// removes notes from bottom of table 
	indicate(`r(indicate_fe)', label("Y" "")) /// adds y & N for FE inclusions
	title(Matched Sample: Heterogeneous Treatment Effects by Capital Expenditures for Building Improvements\label{tab15heteffectsfmatched`i'_2years}) ///
	addnote({\scriptsize * p$<$0.05. The standard errors reported in parenthesis have been clustered at the CBSA level.})

	}
	
	
